/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.restclient;

import cz.muni.fi.pa165.bookingmanager.dto.CustomerDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Kouba
 */
public class CustomerTableModel extends AbstractTableModel {

    static final Logger logger = Logger.getLogger(CustomerTableModel.class.getName());
    private CustomerRestClient customerClient = new CustomerRestClient();
    private List<CustomerDTO> customers = new ArrayList<CustomerDTO>();

    @Override
    public int getRowCount() {
        return customers.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        CustomerDTO customer = customers.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return customer.getFirstName();
            case 1:
                return customer.getSurName();
            case 2:
                return customer.getNumberOfIdCard();
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Firstname";
            case 1:
                return "Surname";
            case 2:
                return "Number of id card";
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public void addCustomer(CustomerDTO customer) {


        customerClient.createCustomer(customer);
        loadData();
         
       
        
        


    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        CustomerDTO customer = customers.get(rowIndex);
        switch (columnIndex) {
            case 0:
                customer.setFirstName((String) aValue);
                break;
            case 1:
                customer.setSurName((String) aValue);
                break;
            case 2:
                customer.setNumberOfIdCard((String) aValue);
                break;
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
        customerClient.updateCustomer(customer);
        loadData();
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
            case 1:
            case 2:
                return true;
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public CustomerDTO getCustomer(Long id) {
        return customerClient.getCustomerById(id);
    }

    public void deleteCustomer(int row) throws IllegalArgumentException {
        CustomerDTO customer = customers.get(row);
        customerClient.deleteCustomer(customer);
        fireTableRowsDeleted(row, row);
      
        customers = customerClient.getCustomers();
        
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public final void loadData() {
      
        customers = customerClient.getCustomers();
        if(!customers.isEmpty())
        {
        int lastRow = customers.size() - 1;
        fireTableRowsInserted(0, lastRow);
        }
    }
    
}
